package question;

/**
 * 问题1：抢电影票
 * @描述 一共有1000张电影票,可以在两个窗口领取,假设每次领取的时间为3000毫秒要求:请用多线程模拟卖票过程并打印剩余电影票的数量
 * @author 阳秋豪
 * @createTime 2024/3/12
 */
public class Test1 {
    public static void main(String[] args) {
        QOneRunnable1 runnable = new QOneRunnable1();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t1.start();
        t2.start();
    }
}

class QOneRunnable1 implements Runnable {
    int ticket = 1000;  // 剩余票数

    @Override
    public void run() {
        while (true) {
            synchronized (QOneRunnable1.class) {
                if (ticket == 0) {
                    break;
                } else {
                    ticket--;
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "出售了1张票还剩" + ticket + "张票");
                }
            }
        }
    }
}